﻿using System.Collections.Generic;
using System.Windows.Media.Imaging;

using Gonte.UI;
using Gonte.Wpf.Controls.ResourceManagers;
using Gonte.Wpf.Controls.ViewModel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyWpfControlsTest
{
    [TestClass()]
    public class ContentObjectViewModelTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        #region Single child type

        [UserInterface(
        LabelProperty = "Name",
        IconUri = @"pack://application:,,/Images/Item.jpg")]
        public class ChildTreeItem
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }

        [UserInterface(
        LabelProperty = "Name",
        CollapsedIconUri = @"pack://application:,,/Images/CollapsedRoot.jpg",
        ExpandedIconUri = @"pack://application:,,/Images/ExpandedRoot.jpg")]
        [UserInterfaceItem(PropertyName = "Children")]
        public class ParentTreeItem
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private List<ChildTreeItem> _children = new List<ChildTreeItem>();

            public List<ChildTreeItem> Children
            {
                get { return _children; }
                set { _children = value; }
            }
        }

        [UserInterface(
        LabelProperty = "Name",
        CollapsedIconUri = @"pack://application:,,/Images/CollapsedRoot.jpg",
        ExpandedIconUri = @"pack://application:,,/Images/ExpandedRoot.jpg")]
        [UserInterfaceItem(PropertyName = "Parents")]
        public class RootTreeItem
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private List<ParentTreeItem> _parents = new List<ParentTreeItem>();

            public List<ParentTreeItem> Parents
            {
                get { return _parents; }
                set { _parents = value; }
            }
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructorTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            ParentTreeItem root = new ParentTreeItem();
            root.Name = "My Root Name";

            const int count = 10;

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();
                root.Children.Add(child);
            }

            // Test the view model info
            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(ParentTreeItem));
            viewModelInfo.LoadOnDemand = false;
            viewModelInfo.SortChildren = false;

            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, root);

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded

            Assert.AreEqual(count, viewModel.Children.Count);
            ItemObjectViewModel childViewModel = (ItemObjectViewModel)viewModel.Children[0];

            Assert.AreEqual("Child #:1", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructorDescendingTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            ParentTreeItem root = new ParentTreeItem();
            root.Name = "My Root Name";

            const int count = 10;

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();
                root.Children.Add(child);
            }

            // Test the view model info
            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(ParentTreeItem));
            viewModelInfo.LoadOnDemand = false;
            viewModelInfo.SortChildren = true;

            ItemObjectViewModelInfo childViewModelInfo = (ItemObjectViewModelInfo)ItemObjectViewModelInfo.GetInfo(typeof(ChildTreeItem));
            childViewModelInfo.ChildrenComparer = delegate(ItemViewModel viewModel1, ItemViewModel viewModel2)
            {
                return -(viewModel1.Label.CompareTo(viewModel2.Label));
            };

            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, root);

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded

            Assert.AreEqual(count, viewModel.Children.Count);
            ItemObjectViewModel childViewModel = (ItemObjectViewModel)viewModel.Children[0];

            Assert.AreEqual("Child #:9", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructorLoadOnDemandTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            ParentTreeItem root = new ParentTreeItem();
            root.Name = "My Root Name";

            const int count = 10;

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();
                root.Children.Add(child);
            }

            // Test the view model info
            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(ParentTreeItem));
            viewModelInfo.LoadOnDemand = true;
            viewModelInfo.SortChildren = false;

            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, root);

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);
            Assert.AreEqual(1, viewModel.Children.Count); // The load on demand child is in place

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded
            Assert.AreEqual(count, viewModel.Children.Count); // After expanding the real children should be loaded

            ItemObjectViewModel childViewModel = (ItemObjectViewModel)viewModel.Children[0];

            Assert.AreEqual("Child #:1", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);
        }

        [TestMethod()]
        public void ContentObjectViewModelSetModelTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            ParentTreeItem root = new ParentTreeItem();
            root.Name = "My Root Name";

            const int count = 10;

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();
                root.Children.Add(child);
            }

            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(ParentTreeItem));
            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo);
            viewModel.Model = root;

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded

            Assert.AreEqual(count, viewModel.Children.Count);
            ItemObjectViewModel childViewModel = (ItemObjectViewModel)viewModel.Children[0];

            Assert.AreEqual("Child #:1", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);

            // Move the last item up
            viewModel.MoveChildUp(root.Children[count - 1]);
            Assert.AreEqual(count, viewModel.Children.Count);
            Assert.AreEqual("Child #:9", ((ItemViewModel)viewModel.Children[count - 1]).Label);
            Assert.AreEqual("Child #:9", root.Children[count - 1].Name);

            // Restore the previous order
            viewModel.MoveChildDown(root.Children[count - 2]);
            Assert.AreEqual(count, viewModel.Children.Count);
            Assert.AreEqual("Child #:10", ((ItemViewModel)viewModel.Children[count - 1]).Label);
            Assert.AreEqual("Child #:10", root.Children[count - 1].Name);

            // Add the item
            ChildTreeItem childItem = new ChildTreeItem();
            childItem.Name = "Added Child";
            viewModel.AddChild(childItem);

            Assert.AreEqual(count + 1, viewModel.Children.Count);
            ItemViewModel itemViewModel = (ItemViewModel)viewModel.Children[count];
            Assert.AreEqual(childItem.Name, ((ChildTreeItem)itemViewModel.Model).Name);

            Assert.AreEqual(count + 1, root.Children.Count);
            Assert.AreEqual(childItem.Name, root.Children[count].Name);

            // Remove the item
            viewModel.RemoveChild(childItem);
            Assert.AreEqual(count, viewModel.Children.Count);
            itemViewModel = (ItemViewModel)viewModel.Children[count - 1];
            Assert.AreEqual("Child #:10", ((ChildTreeItem)itemViewModel.Model).Name);

            Assert.AreEqual(count, root.Children.Count);
            Assert.AreEqual("Child #:10", root.Children[count - 1].Name);
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructor3LevelHierarchyTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object

            RootTreeItem root = new RootTreeItem();
            root.Name = "My Root Name";

            const int parentCount = 5;
            const int childrenCount = 10;

            for (int j = 0; j < parentCount; ++j)
            {

                ParentTreeItem parent = new ParentTreeItem();
                parent.Name = "My Parent Name #:" + (j + 1).ToString(); ;

                for (int i = 0; i < childrenCount; ++i)
                {
                    ChildTreeItem child = new ChildTreeItem();
                    child.Name = "Child #:" + (i + 1).ToString();
                    parent.Children.Add(child);
                }

                root.Parents.Add(parent);
            }

            // Test the view model info
            ContentObjectViewModelInfo rootViewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(RootTreeItem));
            rootViewModelInfo.LoadOnDemand = false;
            rootViewModelInfo.SortChildren = false;

            Assert.AreEqual(collapsedImageUri, rootViewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, rootViewModelInfo.ExpandedIconUri);

            ContentObjectViewModel rootViewModel = new ContentObjectViewModel(rootViewModelInfo, null, root);

            Assert.AreEqual("My Root Name", rootViewModel.Label);

            Assert.AreEqual(false, rootViewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, rootViewModel.IconSource);

            rootViewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, rootViewModel.IconSource); // Verify we get the expanded image when the item is expanded

            Assert.AreEqual(parentCount, rootViewModel.Children.Count);
            ContentObjectViewModel parentViewModel = (ContentObjectViewModel)rootViewModel.Children[0];

            Assert.AreEqual("My Parent Name #:1", parentViewModel.Label);
            Assert.AreEqual(collapsedImage, parentViewModel.IconSource);
            Assert.AreEqual(rootViewModel, parentViewModel.Parent);

            Assert.AreEqual(childrenCount, parentViewModel.Children.Count);
            ItemObjectViewModel childViewModel = (ItemObjectViewModel)parentViewModel.Children[0];

            Assert.AreEqual("Child #:1", childViewModel.Label);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(parentViewModel, childViewModel.Parent);
        }

        #endregion

        [UserInterface(
        LabelProperty = "Name",
        IconUri = @"pack://application:,,/Images/Item2.jpg")]
        public class ChildTreeItem2
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }

        [UserInterface(
        LabelProperty = "Name",
        CollapsedIconUri = @"pack://application:,,/Images/CollapsedRoot.jpg",
        ExpandedIconUri = @"pack://application:,,/Images/ExpandedRoot.jpg")]
        [UserInterfaceItem(PropertyName = "Children")]
        [UserInterfaceItem(PropertyName = "Children2")]
        public class RootTreeItem2
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            private List<ChildTreeItem> _children = new List<ChildTreeItem>();

            public List<ChildTreeItem> Children
            {
                get { return _children; }
                set { _children = value; }
            }

            private List<ChildTreeItem2> _children2 = new List<ChildTreeItem2>();

            public List<ChildTreeItem2> Children2
            {
                get { return _children2; }
                set { _children2 = value; }
            }
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructorMultipleChildrenTypeTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage item2Image = new BitmapImage();
            string item2ImageUri = @"pack://application:,,/Images/Item2.jpg";
            ImageManager_Accessor.Images[item2ImageUri] = item2Image;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            RootTreeItem2 root = new RootTreeItem2();
            root.Name = "My Root Name";

            const int count = 10;

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();
                root.Children.Add(child);
            }

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem2 child = new ChildTreeItem2();
                child.Name = "Child2Type #:" + (i + 1).ToString();
                root.Children2.Add(child);
            }

            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(RootTreeItem2));
            viewModelInfo.LoadOnDemand = false;
            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, root);

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded
            Assert.AreEqual(2, viewModel.Children.Count); // The 2 folder view models containing the 10 children

            FolderViewModel folderViewModel = (FolderViewModel)viewModel.Children[1];

            Assert.AreEqual(count, folderViewModel.Children.Count);

            ItemObjectViewModel childViewModel = (ItemObjectViewModel)folderViewModel.Children[0];
            Assert.AreEqual("Child #:1", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);

            folderViewModel = (FolderViewModel)viewModel.Children[0];

            Assert.AreEqual(count, folderViewModel.Children.Count);

            childViewModel = (ItemObjectViewModel)folderViewModel.Children[0];
            Assert.AreEqual("Child2Type #:1", childViewModel.Label);
            //Assert.AreEqual(item2ImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(item2Image, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructorMultipleChildrenTypeLoadOnDemandTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage item2Image = new BitmapImage();
            string item2ImageUri = @"pack://application:,,/Images/Item2.jpg";
            ImageManager_Accessor.Images[item2ImageUri] = item2Image;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            RootTreeItem2 root = new RootTreeItem2();
            root.Name = "My Root Name";

            const int count = 10;

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();
                root.Children.Add(child);
            }

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem2 child = new ChildTreeItem2();
                child.Name = "Child2Type #:" + (i + 1).ToString();
                root.Children2.Add(child);
            }

            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(RootTreeItem2));
            viewModelInfo.LoadOnDemand = true;
            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, root);

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded
            Assert.AreEqual(2, viewModel.Children.Count); // The 2 folder view models containing the 10 children

            FolderViewModel folderViewModel = (FolderViewModel)viewModel.Children[0];
            Assert.AreEqual(1, folderViewModel.Children.Count); // The folder view model should contain the load on demand placeholder view model

            LoadOnDemandViewModel loadOnDemandViewModel = (LoadOnDemandViewModel)folderViewModel.Children[0]; // Verify we have the placeholders
            folderViewModel.IsExpanded = true;

            Assert.AreEqual(count, folderViewModel.Children.Count); // Verify the real children were loaded

            ItemObjectViewModel childViewModel = (ItemObjectViewModel)folderViewModel.Children[0];
            Assert.AreEqual("Child2Type #:1", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(item2Image, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);

            folderViewModel = (FolderViewModel)viewModel.Children[1]; // Get the second view model folder
            Assert.AreEqual(1, folderViewModel.Children.Count); // The folder view model should contain the load on demand placeholder view model

            loadOnDemandViewModel = (LoadOnDemandViewModel)folderViewModel.Children[0]; // Verify we have the placeholders
            folderViewModel.IsExpanded = true;

            Assert.AreEqual(count, folderViewModel.Children.Count); // Verify the real children were loaded

            childViewModel = (ItemObjectViewModel)folderViewModel.Children[0];
            Assert.AreEqual("Child #:1", childViewModel.Label);
            //Assert.AreEqual(item2ImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);
        }

        [TestMethod()]
        public void ContentObjectViewModelConstructorMultipleChildrenCustomAddChildTest()
        {
            // Set up the images
            BitmapImage itemImage = new BitmapImage();
            string itemImageUri = @"pack://application:,,/Images/Item.jpg";
            ImageManager_Accessor.Images[itemImageUri] = itemImage;

            BitmapImage item2Image = new BitmapImage();
            string item2ImageUri = @"pack://application:,,/Images/Item2.jpg";
            ImageManager_Accessor.Images[item2ImageUri] = item2Image;

            BitmapImage collapsedImage = new BitmapImage();
            string collapsedImageUri = @"pack://application:,,/Images/CollapsedRoot.jpg";
            ImageManager_Accessor.Images[collapsedImageUri] = collapsedImage;

            BitmapImage expandedImage = new BitmapImage();
            string expandedImageUri = @"pack://application:,,/Images/ExpandedRoot.jpg";
            ImageManager_Accessor.Images[expandedImageUri] = expandedImage;

            // Set up the object
            RootTreeItem2 root = new RootTreeItem2();
            root.Name = "My Root Name";

            const int count = 10;

            //for (int i = 0; i < count; ++i)
            //{
            //    ChildTreeItem child = new ChildTreeItem();
            //    child.Name = "Child #:" + (i + 1).ToString();
            //    root.Children.Add(child);
            //}

            //for (int i = 0; i < count; ++i)
            //{
            //    ChildTreeItem2 child = new ChildTreeItem2();
            //    child.Name = "Child2Type #:" + (i + 1).ToString();
            //    root.Children2.Add(child);
            //}

            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(RootTreeItem2));
            viewModelInfo.LoadOnDemand = false;
            Assert.AreEqual(collapsedImageUri, viewModelInfo.CollapsedIconUri);
            Assert.AreEqual(expandedImageUri, viewModelInfo.ExpandedIconUri);

            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, root);

            viewModel.AddItem = delegate(object model, string childrenPropertyName, object child)
            {
                RootTreeItem2 rootTree = (RootTreeItem2)model;

                switch (childrenPropertyName)
                {
                    case "Children":
                        {
                            rootTree.Children.Add((ChildTreeItem)child);
                        }
                        break;
                    case "Children2":
                        {
                            rootTree.Children2.Add((ChildTreeItem2)child);
                        }
                        break;
                }
            };

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem child = new ChildTreeItem();
                child.Name = "Child #:" + (i + 1).ToString();

                viewModel.AddChild("Children", child);
            }

            for (int i = 0; i < count; ++i)
            {
                ChildTreeItem2 child = new ChildTreeItem2();
                child.Name = "Child2Type #:" + (i + 1).ToString();

                viewModel.AddChild("Children2", child);
            }

            Assert.AreEqual("My Root Name", viewModel.Label);

            Assert.AreEqual(false, viewModel.IsExpanded); // The tree is collapsed by default
            Assert.AreEqual(collapsedImage, viewModel.IconSource);

            viewModel.IsExpanded = true;
            Assert.AreEqual(expandedImage, viewModel.IconSource); // Verify we get the expanded image when the item is expanded
            Assert.AreEqual(2, viewModel.Children.Count); // The 2 folder view models containing the 10 children

            FolderViewModel folderViewModel = (FolderViewModel)viewModel.Children[0];
            Assert.AreEqual(count, folderViewModel.Children.Count); // The folder view model should contain the load on demand placeholder view model

            ItemObjectViewModel childViewModel = (ItemObjectViewModel)folderViewModel.Children[0];
            Assert.AreEqual("Child2Type #:1", childViewModel.Label);
            //Assert.AreEqual(itemImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(item2Image, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);

            folderViewModel = (FolderViewModel)viewModel.Children[1]; // Get the second view model folder
            Assert.AreEqual(count, folderViewModel.Children.Count); // Verify the real children were loaded

            childViewModel = (ItemObjectViewModel)folderViewModel.Children[0];
            Assert.AreEqual("Child #:1", childViewModel.Label);
            //Assert.AreEqual(item2ImageUri, childViewModelInfo.IconUri);
            Assert.AreEqual(itemImage, childViewModel.IconSource);
            Assert.AreEqual(viewModel, childViewModel.Parent);
        }
    }
}
